package top.yaofengqiao.springcloudsimple.web.starter.security;

import cn.hutool.core.collection.CollUtil;
import top.yaofengqiao.springcloudsimple.auth.api.dto.TokenAuthResultDTO;
import top.yaofengqiao.springcloudsimple.web.starter.util.OauthUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author yfq
 * @date 2024/4/11 14:31
 * @description 基于内存的权限校验器
 */
public class MemoryPermissionHandler implements PermissionHandler {

    @Override
    public boolean hasRole(String role) {
        TokenAuthResultDTO tokenAuthResultDTO = OauthUtil.currentUser();
        List<String> roles = tokenAuthResultDTO.getRoles();

        return CollUtil.isNotEmpty(roles) && roles.contains(role);
    }

    @Override
    public boolean hasAnyRole(String... roles) {
        TokenAuthResultDTO tokenAuthResultDTO = OauthUtil.currentUser();
        List<String> hasRoles = tokenAuthResultDTO.getRoles();

        return CollUtil.isNotEmpty(hasRoles) && CollUtil.isNotEmpty(CollUtil.intersection(hasRoles, Arrays.asList(roles)));
    }

    @Override
    public boolean hasPermission(String permission) {
        // TODO 暂未实现
        return true;
    }

}
